package com.lanou.zakerproject.subscribe;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.BaseActivity;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.tool.GsonRequest;

/**
 * Created by dllo on 16/1/14.
 */
public class NewsActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView listView;
    private NewsAdapter newsAdapter;
    private String pk;
    private String url;
    private TextView subscribe_listview_news_author;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        listView = (ListView) findViewById(R.id.subscribe_listview_news);
        subscribe_listview_news_author = (TextView) findViewById(R.id.subscribe_listview_news_author);
        Intent intent = getIntent();
        pk = intent.getStringExtra("channel_news_pk");
        url = "http://iphone.myzaker.com/zaker/blog.php?app_id=" + pk;
        getRequsetData(url);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, NewsDetailActivity.class);
        startActivity(intent);
    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<NewsChannelToDetailData> gsonRequest = new GsonRequest<>(urls, NewsChannelToDetailData.class, new Response.Listener<NewsChannelToDetailData>() {
            @Override
            public void onResponse(NewsChannelToDetailData response) {
                newsAdapter = new NewsAdapter(NewsActivity.this, response);
                listView.setAdapter(newsAdapter);
                for (int i = 0; i < response.getData().getArticles().size(); i++) {
                    //位置信息差3
                    subscribe_listview_news_author.setText(response.getData().getArticles().get(i).getAuther_name());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

}
